
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="函数式编程.html" />
    
    
    <link rel="prev" href="数据结构高级内容.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x51FD;&#x6570;">&#x51FD;&#x6570;</h1>
<hr>
<h2 id="&#x6570;&#x636E;&#x7C7B;&#x578B;&#x68C0;&#x67E5;">&#x6570;&#x636E;&#x7C7B;&#x578B;&#x68C0;&#x67E5;</h2>
<p>&#x6570;&#x636E;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x53EF;&#x4EE5;&#x7528;&#x5185;&#x7F6E;&#x51FD;&#x6570; <code>isinstance()</code>&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_abs</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> isinstance(x, (int, float)): <span class="hljs-comment">#&#x5982;&#x679C;&#x4F20;&#x5165;&#x9519;&#x8BEF;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x51FD;&#x6570;&#x5C31;&#x53EF;&#x4EE5;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;</span>
        <span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">&apos;bad operand type&apos;</span>)
    <span class="hljs-keyword">if</span> x &gt;= <span class="hljs-number">0</span>:
        <span class="hljs-keyword">return</span> x
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> -x
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x53C2;&#x6570;">&#x51FD;&#x6570;&#x53C2;&#x6570;</h2>
<p>&#x53D8;&#x91CF;&#x5F15;&#x7528;&#x67E5;&#x627E;&#x987A;&#x5E8F;&#xFF1A;&#x9996;&#x5148;&#x5728;&#x5C40;&#x90E8;&#x7B26;&#x53F7;&#x8868;&#x4E2D;&#x67E5;&#x627E;&#xFF0C;&#x7136;&#x540E;&#x662F;&#x5305;&#x542B;&#x51FD;&#x6570;&#x7684;&#x5C40;&#x90E8;&#x7B26;&#x53F7;&#x8868;&#xFF0C;&#x7136;&#x540E;&#x662F;&#x5168;&#x5C40;&#x7B26;&#x53F7;&#x8868;&#xFF0C;&#x6700;&#x540E;&#x662F;&#x5185;&#x7F6E;&#x540D;&#x5B57;&#x8868;&#x3002;
&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6240;&#x6709;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x90FD;&#x662F;&#x5C06;&#x503C;&#x5B58;&#x50A8;&#x5728;&#x5C40;&#x90E8;&#x7B26;&#x53F7;&#x8868;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x4E0D;&#x80FD;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x76F4;&#x63A5;&#x8D4B;&#x503C;&#xFF08;&#x9664;&#x975E;&#x7528; global &#x8BED;&#x53E5;&#x547D;&#x540D;&#xFF09;&#xFF0C;&#x5C3D;&#x7BA1;&#x4ED6;&#x4EEC;&#x53EF;&#x4EE5;&#x88AB;&#x5F15;&#x7528;&#x3002;</p>
<h3 id="global">global</h3>
<p>&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x62E5;&#x6709;&#x4E00;&#x4E2A;&#x5C40;&#x90E8;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x5916;&#x5B9A;&#x4E49;&#x7684;&#x62E5;&#x6709;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x3002;<br>&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x53EF;&#x4EE5;&#x5F15;&#x7528;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x4F46;&#x65E0;&#x6CD5;&#x5BF9;&#x5176;&#x8D4B;&#x503C;&#xFF08;&#x9664;&#x975E;&#x7528; global &#x8FDB;&#x884C;&#x58F0;&#x660E;&#xFF09;  </p>
<pre><code class="lang-python">x, y = <span class="hljs-number">1</span>, <span class="hljs-number">2</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">global</span> x  <span class="hljs-comment">#&#x58F0;&#x660E;&#x5168;&#x5C40;&#x53D8;&#x91CF;</span>
    x = <span class="hljs-number">10</span>    <span class="hljs-comment">#&#x7ED9;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x91CD;&#x65B0;&#x8D4B;&#x503C;</span>
    y = <span class="hljs-number">20</span>    <span class="hljs-comment">#&#x7ED9;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#xFF0C;&#x5B83;&#x548C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x540C;&#x540D;</span>
func()
print(x, y)   <span class="hljs-comment">#&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;10 2</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#xFF1A;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x540C;&#x4E00;&#x4E2A; global &#x8BED;&#x53E5;&#x6307;&#x5B9A;&#x591A;&#x4E2A;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;&#x4F8B;&#x5982; global x, y, z</p>
</blockquote>
<h3 id="nonlocal">nonlocal</h3>
<p>&#x8FD8;&#x6709;&#x4E00;&#x79CD;&#x4F5C;&#x7528;&#x57DF;&#x53EB;&#x505A;&#x201C;&#x975E;&#x5C40;&#x90E8;&#x201D;&#x57DF;&#xFF0C;&#x5904;&#x4E8E;&#x5168;&#x5C40;&#x548C;&#x5C40;&#x90E8;&#x8FD9;&#x4E24;&#x79CD;&#x4F5C;&#x7528;&#x57DF;&#x4E4B;&#x95F4;&#x3002;<br>&#x975E;&#x5C40;&#x90E8;&#x4F5C;&#x7528;&#x57DF;&#x5728;&#x4F60;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x5185;&#x7684;&#x51FD;&#x6570;&#x65F6;&#x4F1A;&#x770B;&#x5230;&#x3002;  </p>
<pre><code class="lang-python">a = <span class="hljs-number">100</span>  <span class="hljs-comment">#&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x91CC;&#x662F;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func_outer</span><span class="hljs-params">()</span>:</span>
    x = <span class="hljs-number">2</span>  <span class="hljs-comment">#&#x5916;&#x5C42;&#x5C40;&#x90E8;&#x4F5C;&#x7528;&#x57DF;</span>
    print(x)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func_inner</span><span class="hljs-params">()</span>:</span>  
        <span class="hljs-keyword">nonlocal</span> x
        x = <span class="hljs-number">5</span>
    func_inner()
    print(x)
func_outer()  <span class="hljs-comment">#&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;2 5</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#xFF1A;&#x5BF9;&#x4E8E;func_inner &#x7684;&#x5185;&#x90E8;&#x6765;&#x8BF4;&#xFF0C;func_outer&#x4E2D;&#x5B9A;&#x4E49;&#x7684; &#x2019;x&#x2019; &#x65E2;&#x4E0D;&#x662F;&#x5728;&#x5F53;&#x524D;&#x5C40;&#x90E8;&#x8303;&#x56F4;&#x4E5F;&#x4E0D;&#x662F;&#x5728;&#x5168;&#x5C40;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;&#x8FD9;&#x6837;&#x7684; x &#x79F0;&#x4E4B;&#x4E3A;&#x975E;&#x5C40;&#x90E8; x&#x3002;</p>
</blockquote>
<h3 id="&#x4F4D;&#x7F6E;&#x53C2;&#x6570;">&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_max</span><span class="hljs-params">(a, b)</span>:</span>
    <span class="hljs-keyword">pass</span>
print_max(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)
</code></pre>
<h3 id="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;">&#x9ED8;&#x8BA4;&#x53C2;&#x6570;</h3>
<p>&#x6709;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x7684;&#x53C2;&#x6570;&#x4E0D;&#x80FD;&#x4F4D;&#x4E8E;&#x6CA1;&#x6709;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x7684;&#x53C2;&#x6570;&#x4E4B;&#x524D;&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_max</span><span class="hljs-params">(a, b=<span class="hljs-number">2</span>)</span>:</span>
    <span class="hljs-keyword">pass</span>
print_max(<span class="hljs-number">1</span>)
</code></pre>
<p>&#x51FD;&#x6570;&#x9ED8;&#x8BA4;&#x503C;&#x5728;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x70B9;&#xFF08; &#x5373;defining scope&#xFF09;&#x8FDB;&#x884C;&#x8BA1;&#x7B97;&#xFF0C;&#x5E76;&#x4E14;&#x53EA;&#x8BA1;&#x7B97;&#x4E00;&#x6B21;&#x3002;&#x5982;&#x679C;&#x9ED8;&#x8BA4;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x53D8;&#x5BF9;&#x8C61;&#x662F;&#x8981;&#x5C0F;&#x5FC3;&#xFF0C;&#x5728;&#x540E;&#x7EED;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F1A;&#x7D2F;&#x79EF;&#xFF08;&#x524D;&#x9762;&#xFF09;&#x4F20;&#x7ED9;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x3002;<br>&#x4F8B;1&#xFF1A;  </p>
<pre><code class="lang-python">i = <span class="hljs-number">5</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(arg=i)</span>:</span>
    print(arg)
i = <span class="hljs-number">6</span>
f()  <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;5</span>
</code></pre>
<p>&#x4F8B;2&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(a, L=[])</span>:</span>  <span class="hljs-comment">#&#x5982;&#x679C;&#x60F3;&#x907F;&#x514D;&#x7D2F;&#x79EF;&#x95EE;&#x9898;&#xFF0C;&#x5E94;&#x8BE5;&#x5B9A;&#x4E49;L=None</span>
    L.append(a)
    <span class="hljs-keyword">return</span> L
print(f(<span class="hljs-number">1</span>))  <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;[1]</span>
print(f(<span class="hljs-number">2</span>))  <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;[1,2]</span>
print(f(<span class="hljs-number">3</span>))  <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;[1,2,3]</span>
</code></pre>
<blockquote>
<p>&#x5B9A;&#x4E49;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x8981;&#x7262;&#x8BB0;&#x4E00;&#x70B9;&#xFF1A;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x6307;&#x5411;&#x4E0D;&#x53D8;&#x5BF9;&#x8C61;&#xFF0C;&#x5426;&#x5219;&#x5BB9;&#x6613;&#x6389;&#x5165;&#x9677;&#x9631;&#xFF01;&#xFF01;&#xFF01;</p>
</blockquote>
<h3 id="keyword&#x53C2;&#x6570;&#xFF08;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#xFF09;">Keyword&#x53C2;&#x6570;&#xFF08;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#xFF09;</h3>
<p>&#x4F7F;&#x7528;&#x540D;&#x5B57;&#xFF08;&#x5173;&#x952E;&#x5B57;&#xFF09;&#x800C;&#x4E0D;&#x662F;&#x4F4D;&#x7F6E;&#x6765;&#x7ED9;&#x51FD;&#x6570;&#x6307;&#x5B9A;&#x5B9E;&#x53C2;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_max</span><span class="hljs-params">(a, b)</span>:</span>
    <span class="hljs-keyword">pass</span>
print_max(b=<span class="hljs-number">2</span>, a=<span class="hljs-number">1</span>)
</code></pre>
<h3 id="&#x53EF;&#x53D8;&#x53C2;&#x6570;">&#x53EF;&#x53D8;&#x53C2;&#x6570;</h3>
<p>&#x53EF;&#x53D8;&#x53C2;&#x6570;&#x5141;&#x8BB8;&#x4F60;&#x4F20;&#x5165; 0 &#x4E2A;&#x6216;&#x4EFB;&#x610F;&#x4E2A;&#x53C2;&#x6570;&#x3002;</p>
<ul>
<li>&#x5F53;&#x6211;&#x4EEC;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x8BF8;&#x5982; <code>*param</code> &#x7684;&#x661F;&#x53F7;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x4ECE;&#x6B64;&#x5904;&#x5F00;&#x59CB;&#x76F4;&#x5230;&#x7ED3;&#x675F;&#x7684;&#x6240;&#x6709;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x90FD;&#x5C06;&#x88AB;&#x6536;&#x96C6;&#x5E76;&#x6253;&#x5305;&#x6210;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; param &#x7684;&#x5143;&#x7EC4;&#xFF08; tuple&#xFF09;&#xFF1B;  </li>
<li>&#x5F53;&#x6211;&#x4EEC;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x8BF8;&#x5982; <code>**param</code> &#x7684;&#x53CC;&#x661F;&#x53F7;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x4ECE;&#x6B64;&#x5904;&#x5F00;&#x59CB;&#x76F4;&#x81F3;&#x7ED3;&#x675F;&#x7684;&#x6240;&#x6709;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x90FD;&#x5C06;&#x88AB;&#x6536;&#x96C6;&#x5E76;&#x6253;&#x5305;&#x6210;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; param &#x7684;&#x5B57;&#x5178;&#xFF08; dict )&#xFF1B;  </li>
</ul>
<p>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(*args, **kw)</span>:</span>
    print(args)
    print(kw)
</code></pre>
<blockquote>
<p>&#x8FD9;&#x91CC;&#xFF0C;args &#x63A5;&#x6536;&#x7684;&#x662F;&#x4E00;&#x4E2A; tuple&#xFF0C;kw &#x63A5;&#x6536;&#x7684;&#x662F;&#x4E00;&#x4E2A; dict&#x3002;
&#x4F7F;&#x7528;<code>*args</code> &#x548C; <code>**kw</code> &#x662F; Python &#x7684;&#x4E60;&#x60EF;&#x5199;&#x6CD5;&#xFF0C;&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x5176;&#x4ED6;&#x53C2;&#x6570;&#x540D;&#xFF0C;&#x4F46;&#x6700;&#x597D;&#x4F7F;&#x7528;&#x4E60;&#x60EF;&#x7528;&#x6CD5;&#x3002;</p>
</blockquote>
<p>&#x89E3;&#x5305;&#x51FA;&#x53C2;&#x6570;&#x5217;&#x8868;&#xFF1A;<br><code>*</code>&#x64CD;&#x4F5C;&#x7B26;&#x53EF;&#x4EE5;&#x4ECE;&#x5E8F;&#x5217;&#x6216;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x89E3;&#x5305;&#x51FA;&#x53C2;&#x6570;&#xFF1B;<br><code>**</code>&#x64CD;&#x4F5C;&#x7B26;&#x53EF;&#x4EE5;&#x4ECE;dict&#x89E3;&#x5305;&#x51FA;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#xFF1B;  </p>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
b = {<span class="hljs-string">&apos;p1&apos;</span>:<span class="hljs-number">100</span>, <span class="hljs-string">&apos;p2&apos;</span>:<span class="hljs-number">200</span>}
func(*a, **b)
</code></pre>
<h3 id="keyword-only-&#x53C2;&#x6570;">Keyword-only &#x53C2;&#x6570;</h3>
<p>&#x5982;&#x679C;&#x60F3;&#x6307;&#x5B9A;&#x7279;&#x5B9A;&#x7684;&#x5173;&#x952E;&#x53C2;&#x6570;&#x4E3A; keywor-only &#x800C;&#x4E0D;&#x662F;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x5206;&#x9694;&#x7B26;<code>*</code>&#x7533;&#x660E;&#x3002; <code>*</code>&#x540E;&#x9762;&#x7684;&#x53C2;&#x6570;&#x88AB;&#x89C6;&#x4E3A;Keyword-only&#x53C2;&#x6570;&#x3002;<br>keywor-only&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x540D;&#xFF0C;keywor-only&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x6709;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x4ECE;&#x800C;&#x7B80;&#x5316;&#x8C03;&#x7528;&#x3002;&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">person</span><span class="hljs-params">(name, *, city=<span class="hljs-string">&apos;Beijing&apos;</span>, job)</span>:</span>
    print(name, city, job)
person(<span class="hljs-string">&apos;erick&apos;</span>,job=<span class="hljs-string">&apos;engineer&apos;</span>)
person(<span class="hljs-string">&apos;erick&apos;</span>,city=<span class="hljs-string">&apos;sz&apos;</span>,job=<span class="hljs-string">&apos;engineer&apos;</span>)
</code></pre>
<h3 id="&#x53C2;&#x6570;&#x5B9A;&#x4E49;&#x7684;&#x987A;&#x5E8F;">&#x53C2;&#x6570;&#x5B9A;&#x4E49;&#x7684;&#x987A;&#x5E8F;</h3>
<p>&#x53C2;&#x6570;&#x5B9A;&#x4E49;&#x7684;&#x987A;&#x5E8F;&#x5FC5;&#x987B;&#x4F9D;&#x6B21;&#x662F;&#xFF1A;&#x5FC5;&#x9009;&#x53C2;&#x6570;&#x3001;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x3001;&#x53EF;&#x53D8;&#x53C2;&#x6570;/&#x547D;&#x540D;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x3001;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;    </p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f1</span><span class="hljs-params">(a, b, c=<span class="hljs-number">0</span>, *args, **kw)</span>:</span>
    print(<span class="hljs-string">&apos;a =&apos;</span>, a, <span class="hljs-string">&apos;b =&apos;</span>, b, <span class="hljs-string">&apos;c =&apos;</span>, c, <span class="hljs-string">&apos;args =&apos;</span>, args, <span class="hljs-string">&apos;kw =&apos;</span>, kw)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f2</span><span class="hljs-params">(a, b, c=<span class="hljs-number">0</span>, *, d, **kw)</span>:</span>
    print(<span class="hljs-string">&apos;a =&apos;</span>, a, <span class="hljs-string">&apos;b =&apos;</span>, b, <span class="hljs-string">&apos;c =&apos;</span>, c, <span class="hljs-string">&apos;d =&apos;</span>, d, <span class="hljs-string">&apos;kw =&apos;</span>, kw)
</code></pre>
<p>&#x8FD0;&#x884C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
&gt;&gt;&gt; f1(1, 2, c=3)
a = 1 b = 2 c = 3 args = () kw = {}
&gt;&gt;&gt; f1(1, 2, 3, &apos;a&apos;, &apos;b&apos;)
a = 1 b = 2 c = 3 args = (&apos;a&apos;, &apos;b&apos;) kw = {}
&gt;&gt;&gt; f1(1, 2, 3, &apos;a&apos;, &apos;b&apos;, x=99)
a = 1 b = 2 c = 3 args = (&apos;a&apos;, &apos;b&apos;) kw = {&apos;x&apos;: 99}
&gt;&gt;&gt; f2(1, 2, d=99, ext=None)
a = 1 b = 2 c = 0 d = 99 kw = {&apos;ext&apos;: None}
</code></pre><h2 id="return">return</h2>
<p>&#x4E09;&#x8005;&#x7B49;&#x4EF7;&#xFF1A;</p>
<ul>
<li>&#x51FD;&#x6570;&#x672B;&#x5C3E;&#x6CA1;&#x6709;return</li>
<li>&#x51FD;&#x6570;&#x672B;&#x5C3E;&#x53EA;&#x6709;&#x4E00;&#x4E2A;return</li>
<li>return None</li>
</ul>
<h2 id="&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;">&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;</h2>
<p>&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#x6765;&#x5B9E;&#x73B0;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hi</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)  <span class="hljs-comment">#&#x8BED;&#x6CD5;&#x4E0A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; tuple &#x53EF;&#x4EE5;&#x7701;&#x7565;&#x62EC;&#x53F7;&#xFF0C;&#x6240;&#x4EE5;&#x8FD8;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5199;&#xFF1A;  return 1,2</span>
a, b = hi()      <span class="hljs-comment">#&#x591A;&#x4E2A;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x63A5;&#x6536;&#x4E00;&#x4E2A; tuple&#xFF0C;&#x5E8F;&#x5217;&#x89E3;&#x5305;&#x540E;&#x6309;&#x4F4D;&#x7F6E;&#x8D4B;&#x7ED9;&#x5BF9;&#x5E94;&#x7684;&#x503C;</span>
ret=func(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)
print(a, b)     <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;   2 3</span>
print(ret)     <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;  (2,3)</span>
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x6CE8;&#x89E3;&#xFF08;function-annotations&#xFF09;">&#x51FD;&#x6570;&#x6CE8;&#x89E3;&#xFF08;Function Annotations&#xFF09;</h2>
<p>&#x51FD;&#x6570;&#x6CE8;&#x89E3;&#x662F;&#x662F;&#x53EF;&#x9009;&#x7684;&#x6CE8;&#x91CA;&#x4FE1;&#x606F;&#xFF0C;&#x7528;&#x4E8E;&#x63CF;&#x8FF0;&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x548C;&#x8FD4;&#x56DE;&#x503C;&#x3002;<br>Parameter annotation&#x683C;&#x5F0F;&#xFF1A;  <code>&#x5F62;&#x53C2;&#x540D;:&#x6CE8;&#x89E3;&#x8868;&#x8FBE;&#x5F0F;  = &#x5F62;&#x53C2;&#x9ED8;&#x8BA4;&#x503C;</code><br>Return annotations&#x683C;&#x5F0F;&#xFF1A;    <code>-&gt; &#x6CE8;&#x89E3;&#x8868;&#x8FBE;&#x5F0F;</code><br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(name: str, age: int = <span class="hljs-number">100</span>)</span> -&gt; str:</span>
    print(func.__annotations__)
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;ok&apos;</span>
func(<span class="hljs-string">&apos;erick&apos;</span>) 
<span class="hljs-comment">#&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;{&apos;name&apos;: &lt;class &apos;str&apos;&gt;, &apos;age&apos;: &lt;class &apos;int&apos;&gt;, &apos;return&apos;: &lt;class &apos;str&apos;&gt;}</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="数据结构高级内容.html" class="navigation navigation-prev " aria-label="Previous page: 数据结构高级内容">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="函数式编程.html" class="navigation navigation-next " aria-label="Next page: 函数式编程">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"函数","level":"1.6","depth":1,"next":{"title":"函数式编程","level":"1.7","depth":1,"path":"函数式编程.md","ref":"函数式编程.md","articles":[]},"previous":{"title":"数据结构高级内容","level":"1.5","depth":1,"path":"数据结构高级内容.md","ref":"数据结构高级内容.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"函数.md","mtime":"2017-08-23T14:53:46.784Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

